<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');
if(!ican("sysconfig")){
 msg('对不起，你没有权限执行本操作！');
}

$f = isset($_REQUEST['f'])?$_REQUEST['f']:'index';
if(!in_array($f,array('index','save','delmyvar','testmail'))){
	exit('Access Denied!');
}
$m = new ConfigModule;
$m->$f();
class ConfigModule
{
	function index() {
		global $_G;
		foreach( $_G['settings'] as $key => $value ) {
				$site[$key]=$value;
		}
		$newsmtp=unserialize(decrypt($site['mail']['smtp']));
		foreach( $newsmtp['auth_password'] as $k => $v ){
			if( $v ) $newsmtp['auth_password'][$k] = substr($v, 0,2).'********'.substr($v, -2,2) ;
		}
		$mail = unserialize($site['mail']['mail']);
		$sys['menu'] = unserialize($site['sys']['menu']);
		$display = $site['display'];
		$upload = $site['upload'];
		$user = $site['user'];
		
		if ( !isset($site['cachemod']) ) {
			$site['cachemod'] = array();
		} else {
			$site['cachemod'] = explode('|',$site['cachemod']);
		}
		$tz_012 = $tz_011=$tz_010=$tz_09=$tz_08=$tz_07=$tz_06=$tz_05=$tz_04=$tz_035=$tz_3=$tz_2=$tz_01=$tz_0=$tz_1=$tz_2=$tz_3=$tz_35=$tz_4=$tz_45=$tz_5=$tz_55=$tz_6=$tz_7=$tz_8=$tz_9=$tz_95=$tz_10=$tz_11=$tz_12='';
		$temp = str_replace(array('-','.'), '', $site['timezone']);
		$site['timezone'] < 0 ? ${'tz_0'.$temp} = 'selected' : ${'tz_'.$temp} = 'selected';

		include admintemplate('config');
	}
	function save(){
		global $_G;
		$config = array(
			'site' => getGP('site','P','array'),
			'seo' => getGP('seo','P','array'),
			'display' => getGP('display','P','array'),
			'upload' => getGP('upload','P','array'),
			'user' => getGP('user','P','array'),
			'newsmtp' => getGP('newsmtp','P','array'),
			'mail' => getGP('mail','P','array')
		);
	
		if ( isset($config['site']['cachemod']) && is_array($config['site']['cachemod']) ) {
			$cachemod = array();
			foreach ($config['site']['cachemod'] as $val) {
				if ( in_array($val, array('index','category','article')) ) {
					$cachemod[] = $val;
				}
			}
			$config['site']['cachemod'] = implode('|',$cachemod);
		} else {
			$config['site']['cachemod'] = '';
		}
		if ( substr($config['site']['siteurl'], -1) == '/') {
			$config['site']['siteurl'] = substr($config['site']['siteurl'], 0, -1);
		}
		if ( empty($config['display']['pagesize']) ) {
			$config['display']['pagesize'] = 10;
		}
		if ( empty($config['display']['sidebar_num']) ) {
			$config['display']['sidebar_num'] = 10;
		}
		$newsmtpo=unserialize(decrypt($_G['settings']['mail']['smtp']));
		foreach($config['newsmtp']['auth_password'] as $k=>$v){
			if( $newsmtpo['server'][$k] == $config['newsmtp']['server'][$k] && $v == substr($newsmtpo['auth_password'][$k], 0,2).'********'.substr($newsmtpo['auth_password'][$k], -2,2)) $config['newsmtp']['auth_password'][$k] = $newsmtpo['auth_password'][$k] ;
		}
		$newsmtp = encrypt(serialize($config['newsmtp']));
		$config['newsmtp'] = null;
		$mail = my_serialize($config['mail']);
		$config['mail'] = null;
		$config['mail']['mail'] = $mail ;
		$config['mail']['smtp'] = $newsmtp;
		if ($config['seo']['rewrite'] == 1) {
			$base_root = str_replace('admin.php','',$_SERVER['PHP_SELF']);
			$content = "
	<IfModule mod_rewrite.c>
		RewriteEngine on
		RewriteBase $base_root
		RewriteCond %{QUERY_STRING} ^(.*)$
		
		RewriteRule ^p([0-9]+)$ index.php?page=$1
		RewriteRule ^([A-Za-z]+)_([0-9]+)$ index.php?alias=$1&page=$2
		RewriteRule ^([\w+-\-\w+]+)/p([0-9]+)$ index.php?alias=$1&page=$2
		RewriteRule ^([0-9]+)$ article.php?id=$1
		RewriteRule ^(\w+)\.htm$ index.php?m=page&url=$1
		RewriteRule ^(\w+)\.html$ $1.php
		RewriteRule ^go\/(\w+)$ go/index.php?go=$1
		
		RewriteRule ^(\w+)-([0-9]+)\.html$ $1.php?id=$2
		RewriteRule ^(\w+)-([0-9]+)-([0-9]+)\.html$ $1.php?id=$2&page=$3
		RewriteRule ^(\w+)-page-([0-9]+)\.html$ $1.php?page=$2
		
		RewriteRule ^(\w+)-([a-z\_]+)-([0-9]+)-k-([0-9a-zA-Z\-_=]+)\.html $1.php?f=$2&uid=$3&k=$4
		RewriteRule ^(\w+)-([a-z\_]+)\.html$ $1.php?f=$2
		RewriteRule ^(\w+)-(\w+)-(\w+)\.html$ $1.php?f=$2&a=$3
		
		RewriteRule ^(\w+)-(\w+)-gid-([0-9]+)\.html$ $1.php?f=$2&gid=$3
		RewriteRule ^(\w+)-(\w+)-(\w+)-([0-9]+)\.html$ $1.php?f=$2&a=$3&id=$4
		RewriteRule ^(\w+)-(\w+)-(\w+)-([0-9]+)-([0-9]+)\.html $1.php?f=$2&a=$3&ajax=$4&id=$5
		
		RewriteRule ^(\w+)-(\w+)-(\w+)-t-(\w+)\.html $1.php?f=$2&a=$3&t=$4
		RewriteRule ^(\w+)-(\w+)-(\w+)-t-(\w+)-1\.html $1.php?f=$2&a=$3&t=$4&ajax=1
		RewriteRule ^(\w+)-(\w+)-(\w+)-t-(\w+)-1-([0-9]+)\.html $1.php?f=$2&a=$3&t=$4&ajax=1&id=$5
		RewriteRule ^([\w+-\-\w+]+)$ index.php?alias=$1
	</IfModule>";
			file_put_contents(BASE_ROOT.'.htaccess',$content);
		} else {
			@unlink(BASE_ROOT.'.htaccess');
		}
		if ( !empty($config['seo']['htmldir']) && !preg_match('/^[a-z0-9\{\}\-_\/]+$/i', $config['seo']['htmldir']) ) {
			unset($config['seo']['htmldir']);
		}
		foreach ($config as $type => $vars) {
			if($type=='site') {$type='';}
			foreach ($vars as $key => $val) {
				$key = check_str($key);
				$val = $val;
				if ($key == 'closed_reason') $val = nl2br($val);
				$_G['db']->insert('settings',array('name'=>$key,'value'=>$val,'type'=>$type),true);
			}
		}
		recache('settings');
		msg('成功更改配置信息，你可能需要刷新一下缓存！');
	}
	function delmyvar(){
		global $_G;
		$varname=getGP('varname');
		if ( settings_delete($varname) ) {
			$output = array('type'=>'success');
		} else {
			$output = array('type'=>'error','删除失败！');
		}
		exit(json_encode($output));
	}
	function testmail(){
		if(!ISADMIN)exit;
		$toemail=getGP('e');
		if(is_email( $toemail )) {
			$status = sendmail($toemail,get_email('test','title'),get_email('test','body'));
		}else{
			exit('邮箱不正确');
		}
		if($status){
			exit("邮件已经发送到$toemail ，请查收");
		}else{
			exit("发送失败，请查看错误信息:".BASE_URL.DATA_DIR.'logs/SMTP.txt');
		}
	}
}	
?>